package com.deng.maintenance.vote.view
{
	import com.deng.maintenance.vote.vo.VoteValueVo;
	import com.deng.permission.model.PermissionModelLocator;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	import mx.controls.CheckBox;
	import mx.events.CloseEvent;
	import mx.events.FlexEvent;
	import mx.resources.ResourceManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;

	public class VoteValueValidCB extends CheckBox
	{
		private var pvo:VoteValueVo;
		public function VoteValueValidCB()
		{
			this.addEventListener(FlexEvent.CREATION_COMPLETE,showHandler);
			this.addEventListener(MouseEvent.MOUSE_UP,upEvt);
			this.addEventListener(FlexEvent.SHOW,showHandler);
			super();
		}
				
		private function upEvt(event:MouseEvent):void{			
			if(this.selected){//之前选中的
				Alert.show(ResourceManager.getInstance().getString("lang", "valid_done"));
			}else{//之前没选中，后面要增加
				Alert.show(ResourceManager.getInstance().getString("lang", "cannot_be_replaced_if_valid_do_or_not"), 
					ResourceManager.getInstance().getString("lang", "warn"), 
					Alert.YES | Alert.NO, null, onAlertClose);
			}			
		}
		private function onAlertClose(event:CloseEvent):void{
			if(event.detail == Alert.YES){
				var ro:RemoteObject = new RemoteObject();
				ro.destination = "VoteValueImpl";
				ro.addEventListener(FaultEvent.FAULT,flt);
				ro.addEventListener(ResultEvent.RESULT,updateRst);
				pvo.valid_user_code = PermissionModelLocator.getInstance().userVo.userName;
				ro.validData(pvo);
			}else{				
				this.selected = false;
				this.commitProperties();
			}
		}
		private function updateRst(rst:ResultEvent):void{
			pvo.valid = 1;
			this.enabled = false;
			Alert.show(ResourceManager.getInstance().getString("lang", "valid_success"));
		}
		private function flt(evt:FaultEvent):void{
			Alert.show(evt.fault.getStackTrace());
		}
		private function showHandler(event:FlexEvent):void{
			pvo = this.data as VoteValueVo;
			if( pvo.valid == 1){
				this.selected = true;
				this.enabled = false;
			}else{
				this.selected = false;
			}
		}
	}
}